<?php
/**
 * 这个类配置浙大紫金港二手书B2C业务的控制器
 */
class Zjub2c extends Controller
{

    function Zjub2c()
	{
		parent::Controller();
        $this->load->model('book_model');
        $this->load->model('user_model');
        $this->load->model('post_model');
        $this->load->model('school_model');
        $this->load->model('order_model');
        $this->load->library('session');
        $this->load->library('mypagination');
        //$this->output->enable_profiler(TRUE); //CI的自动报告网站状态函数
        //$this->output->cache(1);   //cache the page for n minutes
	}

    function booklist(){
      if(($this->session->userdata('username'))==""){
          $data['title'] = "书易Bookiki-浙大二手书B2C"; //浏览器标题栏标题
	      $data['activetab']="home"; //导航栏上显示为绿色的tab
          list($schoolsetstatus,$school)=$this->user_model->get_schoolinitial();
          $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
          $data['books']=$this->book_model->get_booklist();
          $this->load->view('zjub2c/booklist_tologin', $data);
          return;
      }
      $data['title'] = "书易Bookiki-浙大二手书B2C"; //浏览器标题栏标题
	  $data['activetab']="home"; //导航栏上显示为绿色的tab
      list($schoolsetstatus,$school)=$this->user_model->get_schoolinitial();
      $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
      $data['books']=$this->book_model->get_booklist();
      $this->load->view('zjub2c/booklist', $data);
    }


    /**
     *订单的收货人，联系方式，配送方式选择完善
     * @return <type>
     */
    function order_additional(){
        if(!empty($_POST["bookid"])){
           $data['bookid']= $_POST["bookid"];
            //$data['baseurl']=base_url();
            $data['title']="书易Bookiki-补充订单信息";            
            $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
            $data['activetab']="none";
            $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
            $this->load->view('zjub2c/order_additional',$data);		//载入注册页面视图
        }
        else
        {
            $this->booklist();
        }
    }

    function order_confirm(){
        $userinfo=$this->user_model->get_userinfo($this->session->userdata('username'));
        $client = null;
        switch($userinfo->campus_id){
            case 1: $client->address="玉泉".$userinfo->dorm;break;
            case 2:$client->address="紫金港".$userinfo->dorm;break;
            case 3: $client->address="西溪".$userinfo->dorm;break;
            case 4: $client->address="华家池".$userinfo->dorm;break;
            case 5: $client->address="之江".$userinfo->dorm;break;
        }
        $client->consignee=$userinfo->name;
        $client->phone=$userinfo->phone;
        $client->short_phone=$userinfo->short_phone;
        $client->dorm=$userinfo->dorm;
        $changed=0;
        if( array_key_exists('consignee',$_POST)){
            $changed=1;
            $client->consignee = $_POST['consignee'];
        }
        if( array_key_exists('dorm',$_POST)){
            $changed=1;
            $client->dorm = $_POST['dorm'];
            $client->address.=$_POST['dorm'];
        }
        if( array_key_exists('phone',$_POST)){
            $changed=1;
            $client->phone = $_POST['phone'];
        }
        if(array_key_exists('short_phone',$_POST)){
            $changed=1;
            $client->short_phone=$_POST['short_phone'];
        }
        $client->user_id = $this->session->userdata('username');
        if($changed==1) $this->user_model->update_profile_forb2c($client);
        $data['client']=$client;
        $data['bookid']=$_POST['bookid'];
        $data['order_books']=$this->order_model->preview_order($_POST['bookid']);
        $data['deliver_type']=$_POST['deliver_type'];
        $data['title']="书易Bookiki-确认订单信息";
        $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('zjub2c/order_confirm',$data);		
    }

    function order_confirm_handle(){
        $data['bookid']=$_POST['bookid'];
        $order_info=$this->order_model->preview_order($_POST['bookid']);
        $deliver_type=$_POST['deliver_type'];
        $total_money=$_POST['total_money'];
        $client=$_POST['client'];
        $data['order_id']=$this->order_model->gen_order($client,$order_info,$total_money,$deliver_type);
        $data['title']="书易Bookiki-订单回执";
        $data['userinfo']=$this->user_model->get_userinfo($this->session->userdata('username'));
        $data['activetab']="none";
        $data['user_status']=$this->user_model->get_user_status(); //获取用户登录状态
        $this->load->view('zjub2c/invoice',$data);		//载入注册页面视图
    }

}
?>
